/*
"JSFXGEN" - UIButton.as
Modular Integrated Development Environment for JesuSonic
Project page: http://jsfxgen.googlecode.com

Copyright 2009 Lubomir I. Ivanov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package neolit123.ui
{
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFormat;
  import flash.text.TextFieldAutoSize;
  import flash.events.MouseEvent;  
  import flash.geom.ColorTransform;
  // =========================================================
  public class UIButton extends Sprite
  {  
    private var cont:Sprite;
    private var bkg:Box;    
    private var dfill:int;
    private var ct:ColorTransform;
    private var toggled:Boolean;
    private var talign:String;
    private var thmcolor0:int = 0x0072ba; //0x9ad8ff
    private var thmcolor1:int = 0x000000; //0xceecff
    private var tf:TextFormat = new TextFormat("_sans", 11);
    private var txt:TextField = new TextField;
    public var data:*;
    public var id:int;
    public var label:String;
    /*
    =========================================================
    parameters are:
    label, width, height, textalign, toggle, textcolor, fillcolor, bordercolor, shadow, shadowdistanc
    */
    public function UIButton(...p:Array):void
    {
      // @ cacheAsBitmap = true;
      //cacheAsBitmap = true;
      // @ get properties
      if (p[0] == undefined || p[0] == null) p[0] = "Label";
      if (p[1] == undefined || p[1] == null) p[1] = 100; 
      if (p[2] == undefined || p[2] == null) p[2] = 25;
      if (p[3] == undefined || p[3] == null) p[3] = "center";
      if (p[4] == undefined || p[4] == null) p[4] = false;
      if (p[5] == undefined || p[5] == null) p[5] = 0xffffff;
      if (p[6] == undefined || p[6] == null) p[6] = thmcolor0;
      if (p[7] == undefined || p[7] == null) p[7] = 0x000000;
      if (p[8] == undefined || p[8] == null) p[8] = true;
      if (p[9] == undefined || p[9] == null) p[9] = 4;
      // @ create container
      cont = new Sprite();
      addChild(cont);
      cont.buttonMode = true;
      cont.doubleClickEnabled = true;
      cont.mouseChildren = false;
      // @ create bkg
      bkg = new Box(p[1],p[2],p[6],p[7],p[8],p[9]);
      cont.addChild(bkg);
      // @ create text
      cont.addChild(txt);
      with (txt)
      {
        multiline = true;
        wordWrap = false;
        talign = align = autoSize = p[3];
        textColor = p[5];
        selectable = false;
        defaultTextFormat = tf;
      }
      setlabel(p[0]);
      setsize(bkg.bkg.width,bkg.bkg.height);
      // @ rollover / rollout
      cont.addEventListener(MouseEvent.MOUSE_OVER, rollover);
      cont.addEventListener(MouseEvent.MOUSE_OUT, rollout);
      ct = bkg.fill.transform.colorTransform;
      // @ is toggle
      if (p[4])
      {
        toggled = false;
        cont.addEventListener(MouseEvent.CLICK, togglef);
        function togglef(e:MouseEvent=null):void {          
          toggle(toggled);
        }
      }
    }
    // =========================================================
    // [ rollover
    // =========================================================
    private function rollover(e:MouseEvent=null):void
    {
       ct.color = thmcolor1;
       bkg.fill.transform.colorTransform = ct;
    }
    // =========================================================
    // [ rollout
    // =========================================================
    private function rollout(e:MouseEvent=null):void
    {
      ct.color = thmcolor0;
      bkg.fill.transform.colorTransform = ct; 
    }
    // =========================================================
    // [ togglef
    // =========================================================
    public function toggle(tgl:Boolean):void
    {
      if (!tgl)
      {
        toggled = true;
        ct.color = thmcolor1;
        bkg.fill.transform.colorTransform = ct;
        cont.removeEventListener(MouseEvent.MOUSE_OVER, rollover);
        cont.removeEventListener(MouseEvent.MOUSE_OUT, rollout);
      }
      else
      {
        toggled = false;
        ct.color = thmcolor0;
        bkg.fill.transform.colorTransform = ct;
        cont.addEventListener(MouseEvent.MOUSE_OVER, rollover);
        cont.addEventListener(MouseEvent.MOUSE_OUT, rollout);
      }
    }
    // =========================================================
    // [ set btn label
    // =========================================================
    public function setlabel(str:String):void
    {
      txt.text = str;
    }
    // =========================================================
    // [ set size
    // =========================================================
    public function setsize(...p:Array):void
    {
      if (p[0] != undefined && p[0] != null) bkg.setsize(p[0],null); 
      if (p[1] != undefined && p[1] != null) bkg.setsize(null,p[1]);
      if (talign == "center")
      {
        txt.x = (bkg.bkg.width - txt.width)*.5;
      }
      else
      {
        txt.x = 3;
      }
      txt.y = (bkg.bkg.height - txt.height)*.5;
    }
    // =========================================================
    // [ enable / disable
    // =========================================================
    public function enable(st:Boolean):void
    {
      if (st) alpha = 1 else alpha = 0.5;
      mouseEnabled = buttonMode = mouseChildren = bkg.shadow.visible = st;
    }
  }
}